package org.qloudgen.borg.pages.frame;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.extensions.markup.html.repeater.tree.DefaultNestedTree;
import org.apache.wicket.extensions.markup.html.repeater.tree.content.Folder;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.qloudgen.borg.beans.MenuNode;
import org.qloudgen.borg.pages.provider.MenuProvider;

public class LeftFrame extends WebPage
{
    private static final long serialVersionUID = 1L;

    private DefaultNestedTree<MenuNode> tree;
    private MenuProvider provider = new MenuProvider();
    
    public LeftFrame(final BodyFrame bodyFrame)
    {
    	
         tree =  new DefaultNestedTree<MenuNode>("tree", provider){
            
        	private static final long serialVersionUID = 1L;

			@Override
			protected Component newContentComponent(String id,IModel<MenuNode> model) {
				return new Folder<MenuNode>(id, tree, model){
						private static final long serialVersionUID = 1L;

						@Override
						protected MarkupContainer newLinkComponent(String id, IModel<MenuNode> model)
						{
							MenuNode menuNode = model.getObject();

							PageParameters parameters = new PageParameters();
							parameters.add("node", menuNode.getId());
							
							if (menuNode.getPageClass() == null){
								return super.newLinkComponent(id, model);
							} else {						
								BookmarkablePageLink<Void> link = new BookmarkablePageLink<Void>(id, menuNode.getPageClass(), parameters);
								link.add(new AttributeModifier("target",new Model<String>("right")));								
								
								return link;
							}
						}
				  };
			}
			
         };
        
         add(tree);

    }

  
    public boolean isVersioned()
    {
        return false;
    }
}